#include "user_uart.h"


uint8_t uart_dma_buffer[BUFFER_SIZE];//DMA数据缓冲区
uint8_t uart_read_buffer[BUFFER_SIZE];//待处理数据缓冲区
uint8_t uart_dma_flag;//DMA数据搬运完成标志位
int x,y,num;
uint8_t cam_dma_buffer[BUFFER_SIZE];//摄像头数据缓冲区
uint8_t cam_read_buffer[BUFFER_SIZE];//摄像头数据待处理缓冲区
uint8_t cam_dma_flag;//DMA数据搬运完成标志位
void user_uart_init(void)
{
    //打开DMA接收
    HAL_UARTEx_ReceiveToIdle_DMA(&huart1,uart_dma_buffer,sizeof(uart_dma_buffer));
    __HAL_DMA_DISABLE_IT(&hdma_usart1_rx,DMA_IT_HT);//关闭DMA半中断

    HAL_UARTEx_ReceiveToIdle_DMA(&huart5,cam_dma_buffer,sizeof(cam_dma_buffer));
    __HAL_DMA_DISABLE_IT(&hdma_uart5_rx,DMA_IT_HT);
}
/**
 * @brief 串口发送重定向
 * 
 * @param huart 
 * @param format 
 * @param ... 
 * @return int 
 */
int my_printf(UART_HandleTypeDef *huart, const char *format, ...)
{
	char buffer[512]; // 临时存储格式化后的字符串
	va_list arg;      // 处理可变参数
	int len;          // 最终字符串长度

	va_start(arg, format);
	// 安全地格式化字符串到 buffer
	len = vsnprintf(buffer, sizeof(buffer), format, arg);
	va_end(arg);

	// 通过 HAL 库发送 buffer 中的内容
	HAL_UART_Transmit(huart, (uint8_t *)buffer, (uint16_t)len, 0xFF);
	return len;
}

/**
 * @brief 串口接收空闲中断
 * 
 * @param huart 
 * @param Size 
 */
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart,uint16_t Size)
{
    if(huart->Instance == USART1)
    {
        //my_printf(&huart5,"dma suc\r\n");
        //关闭DMA接收,防止缓冲区数据出错
        HAL_UART_DMAStop(&huart1);
        //将dma缓冲区数据放入读取缓冲区
        memcpy(uart_read_buffer,uart_dma_buffer,sizeof(uart_dma_buffer));
        //拉高数据缓存成功标志位
        uart_dma_flag = 1;
        //清空dma缓冲区为下一次接收做准备
        memset(uart_dma_buffer,0,sizeof(uart_dma_buffer));
        //再次打开dma为下一次接收做准备
        HAL_UARTEx_ReceiveToIdle_DMA(&huart1,uart_dma_buffer,sizeof(uart_dma_buffer));
        __HAL_DMA_DISABLE_IT(&hdma_usart1_rx,DMA_IT_HT);
    }
    else if(huart->Instance == UART5)
    {
        //关闭DMA接收,防止缓冲区数据出错
        HAL_UART_DMAStop(&huart5);
        //将dma缓冲区数据放入读取缓冲区
        memcpy(cam_read_buffer,cam_dma_buffer,sizeof(cam_dma_buffer));
        //拉高数据缓存成功标志位
        cam_dma_flag = 1;
        //清空dma缓冲区为下一次接收做准备
        memset(cam_dma_buffer,0,sizeof(cam_dma_buffer));
        //再次打开dma为下一次接收做准备
        HAL_UARTEx_ReceiveToIdle_DMA(&huart5,cam_dma_buffer,sizeof(cam_dma_buffer));
        __HAL_DMA_DISABLE_IT(&hdma_uart5_rx,DMA_IT_HT);
    }
}

/**
 * @brief 串口任务函数
 * @param none
 * @return none
 */
extern uint8_t sys_flag;
uint8_t count;
void cam_task(void)
{
    //my_printf(&huart5,"uart task suc\r\n");
    //如果读取缓冲区无数据则直接返回
    if(uart_dma_flag == 0) return;
	//数据解析部分
    //my_printf(&huart5,"data com suc\r\n");
    uint8_t result = sscanf(uart_read_buffer,"xerr=%d,yerr=%d%n",&x,&y,&num);
    if(result == 2 && strlen(uart_read_buffer) == num)
    {
        my_printf(&huart5,"验证:(%d,%d)\r\n",x,y);
        Step_Motor_Set_Speed_my(0,0,2);
        pid_run = 1;
        sys_flag = 1;
    }
    if(strcmp(uart_read_buffer,"null") == 0)
    {
        //Step_Motor_Set_Speed_my(10,0);
        if(++count == 5)
        {
            pid_run = 0;
            count = 0;
        }
        
    }
    //状态重置,为下一次解析做准备
    memset(uart_read_buffer,0,sizeof(uart_read_buffer));
    uart_dma_flag = 0;
}
/**
 * @brief 摄像头数据处理函数
 * 
 */
void uart_task(void)
{
    //如果读取缓冲区无数据则直接返回
    if(cam_dma_flag == 0) return;
	//数据解析部分
    int x,y,num;
    uint8_t result = sscanf(cam_read_buffer,"(%d,%d)%n",&x,&y,&num);
    if(result == 2 && strlen(cam_read_buffer) == num)
    {
      // my_printf(&huart5,"验证:(%d,%d)\r\n",x,y);
    }
    else
    {
        my_printf(&huart5,"error!\r\n");
    }
    //状态重置,为下一次解析做准备
    memset(cam_read_buffer,0,sizeof(cam_read_buffer));
    cam_dma_flag = 0;
}